package edu.mju.ddc.controller.sysadmin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mju.ddc.domain.Book;
import edu.mju.ddc.domain.Category;
import edu.mju.ddc.service.BookService;
import edu.mju.ddc.service.BookServiceImpl;
import edu.mju.ddc.service.CategoryService;
import edu.mju.ddc.service.CategoryServiceImpl;

public class BookMgr extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BookMgr() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BookService bookService = new BookServiceImpl();
		
		request.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act").trim();
		
		if("newBook".equals(act)){
			
			CategoryService ctgService = new CategoryServiceImpl();
			request.setAttribute("ctgs", ctgService.loadall());
			
			request.getRequestDispatcher("../sysadmin/book/input_book.jsp").forward(request, response);
		} else if ("create".equals(act)) {
			String bookCode = request.getParameter("bookcode");
			String bookName = request.getParameter("bookname");
			Double bookPrice = Double.parseDouble(request.getParameter("bookprice"));
			String bookAuthor = request.getParameter("bookauthor");
			String bookPublisher = request.getParameter("bookpublisher");
			Integer bookCtg = Integer.parseInt(request.getParameter("bookCtg"));
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date bookPubDate = null;
			try {
				bookPubDate = sdf.parse(request.getParameter("bookpubdate"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			Book book = new Book();
			book.setBookCode(bookCode);
			book.setBookName(bookName);
			book.setBookPrice(bookPrice);
			book.setBookAuthor(bookAuthor);
			book.setBookPublisher(bookPublisher);
			book.setBookPubDate(bookPubDate);	
			
			Category ctg = new Category();
			ctg.setCtgId(bookCtg);			
			book.setCtg(ctg);
			
			bookService.newBook(book);
			
			response.sendRedirect("bookMgr?act=loadall");
		} else if ("loadall".equals(act)) {
			
			List<Book> books = bookService.loadall();
			
			request.setAttribute("books", books);
			
			request.getRequestDispatcher("../sysadmin/book/list_books.jsp").forward(request, response);
		} else if ("delete".equals(act)) {
			String bookCode = request.getParameter("bookcode");
			bookService.removeBook(bookCode);
			response.sendRedirect("bookMgr?act=loadall");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
